﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using TrainSimulator;
using TrainSimulator.Interfaces;
using TrainSimulator.Model.Track;
using TrainSimulator.Interfaces.DataGridView;

namespace TrainSimulatorGUI
{
    public partial class MainForm : Form
    {
        private static Simulator _simulator;

        public MainForm()
        {
            InitializeFormComponent();
            InitializeSimulation();
            // now some info
            InitializeTrackInfo();
            InitializeTrainInfo();
        }

        private void InitializeFormComponent()
        {
            InitializeComponent();
            toolStrip1.Renderer = new MyToolStripSystemRenderer();
            menuStrip1.Renderer = new MyToolStripSystemRenderer();
        }

        private void InitializeSimulation()
        {
            _simulator = Simulator.Instance;
            simulatorPanel1.DrawSimulation(_simulator);
        }

        private void InitializeTrackInfo()
        {
            dgvTracks.DataSource = _simulator.Tracks.ToList<IDgvTrack>();

            var columns = new DataGridViewColumn[dgvTracks.Columns.Count];
            dgvTracks.Columns.CopyTo(columns, 0);
            int width = 0;
            foreach (var c in columns)
            {
                c.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                width += c.Width;
            }
            if (width > dgvTracks.Width)
                dgvTracks.Columns[dgvTracks.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }

        private void InitializeTrainInfo()
        {
            dgvTrains.DataSource = _simulator.Trains.ToList<IDgvTrain>();

            DataGridViewColumn[] columns = new DataGridViewColumn[dgvTrains.Columns.Count];
            dgvTrains.Columns["RequestedSpeed"].HeaderText = "Req. Speed";
            dgvTrains.Columns["CurrentState"].HeaderText = "State";
            dgvTrains.Columns["WagonsInfo"].HeaderText = "Num. Wagons";
            dgvTrains.Columns.CopyTo(columns, 0);
            int width = 0;
            foreach (var c in columns)
            {
                c.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                width += c.Width;
            }
            if (width > dgvTracks.Width)
                dgvTrains.Columns[dgvTrains.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

        }

        #region Buttons...

        private void showTrackInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (simulatorPanel1.TrackInfo)
                simulatorPanel1.TrackInfo = false;
            else
                simulatorPanel1.TrackInfo = true;
            simulatorPanel1.DrawSimulation(_simulator);
        }

        private void btnStartSimulation_Click(object sender, EventArgs e)
        {
            _simulator.Start();
        }

        #endregion

        

    }
    internal class MyToolStripSystemRenderer : ToolStripSystemRenderer
    {
        protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
        {
            //base.OnRenderToolStripBorder(e);
        }
    }
}
